package top.zackyoung.bbs.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.zackyoung.bbs.bean.ErrorView;
import top.zackyoung.bbs.entity.*;
import top.zackyoung.bbs.mapper.*;
import top.zackyoung.bbs.service.UserService;

import java.util.List;


/**
 * @author: ZackYoung
 * @time: 2020/6/7
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FocusMapper focusMapper;
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private KeepMapper keepMapper;
    @Autowired
    private LikeMapper likeMapper;
    public int addUser(User user){
        return userMapper.addUser(user);
    }
    public User queryUserInformation(String username){
        User user = userMapper.queryUser(username);
        return user;
    }

    public User login(String username, String password) {
        User user = userMapper.queryUser(username);
//        User list = queryUser(username);
        if (user == null) {
            throw new RuntimeException("用户名错误");
        }

        if (!user.getPassword().equals(password)) {
            throw new RuntimeException("密码错误");
        }
        return queryUser(username);
    }

    public User queryUser(String username) {
        User user = userMapper.queryUser(username);
        int id = user.getId();
        List<Focus> focusList = queryFocus(id);
        user.setFocus_num(String.valueOf(focusList.size()));
        user.setFace_num(String.valueOf(queryFaceNum(null,username)));
        return user;
    }

    @Override
    public int updateAvatar(String avatarName, Integer id) {
        int result = userMapper.updateAvatar(avatarName, id);
        return result;
    }

    @Override
    public String updateUser(User user, String password, String oldPassword, String nikname, String allowUserDynamic) {
        if (!oldPassword.equals("") && !password.equals("")) {
            if (password.equals(oldPassword)) {
                return "新密码不能与原密码相同";
            }
            if (password.length() < 6) {
                return ErrorView._801.getContent();
            }
            if (user.getPassword().equals(password)) {
                return ErrorView._802.getContent();
            }
            if (!password.equals(oldPassword)) {
                userMapper.updatePassword(password, user.getId());
                user.setPassword(password);
            }
        }
        if (!nikname.equals("")) {
            if (userMapper.queryNikeName(nikname) > 0) {
                return ErrorView._814.getContent();
            }
            userMapper.updateNikeName(nikname, user.getId());
            user.setNikename(nikname);
        }
        if (!allowUserDynamic.equals("")) {
            if (allowUserDynamic.equals("true")) {
                userMapper.updateAllowUserDynamic("1", user.getId());
                user.setAllowUserDynamic(1);
            }
            if (allowUserDynamic.equals("false")) {
                userMapper.updateAllowUserDynamic("0", user.getId());
                user.setAllowUserDynamic(0);
            }
        }

        return "修改成功";
    }

    @Override
    public List<Focus> queryFocus(Integer id) {
        List<Focus> list = focusMapper.queryFocus(id);
        return list;
    }

    @Override
    public int queryFaceNum(Integer id,String username) {
        int num = focusMapper.queryFaceNum(id,username);
        return num;
    }

    @Override
    public int deleteFocus(Integer id) {
        int num = focusMapper.deleteFocus(id);
        return num;
    }

    @Override
    public int insetFocus(Integer userId, String type, String username) {
        if (username==null){
            return 0;
        }
        int result = focusMapper.insetFocus(userId,type,username);
        return result;
    }

    @Override
    public int queryUserLoginLogCount(Integer id) {
        int result = logMapper.queryUserLoginLogCount(id);
        return result;
    }

    @Override
    public List<Log> queryUserLoginLog(Integer id, Integer page, Integer count) {
        List<Log> list = logMapper.queryUserLoginLog(id,page,count);
        return list;
    }

    @Override
    public int insertKeep(Integer id, Integer type, Integer keep_object_id) {
        if (queryAlreadyKeep(id,type,keep_object_id)>0){
            return 0;
        }
        int result = keepMapper.insertKeep(id,type,keep_object_id);
        return result;
    }

    @Override
    public int queryAlreadyKeep(Integer id, Integer type, Integer keep_object_id) {
        int result = keepMapper.queryAlreadyCollected(id,type,keep_object_id);
        return result;
    }

    @Override
    public int queryIsFocus(Integer id, String type, String focusObjectId) {
        User user = queryUser(focusObjectId);
        int result = focusMapper.queryIsFocus(id,type,user.getId());
        return result;
    }

    @Override
    public int queryLikeCount(Integer userId, Integer type, Integer likeObjectId) {
        int result = likeMapper.queryLikeCount(userId,type,likeObjectId);
        return result;
    }

    @Override
    public int insertLike(Integer userId, Integer type, Integer likeObjectId) {
        int result = likeMapper.insertLike(userId,type,likeObjectId);
        return result;
    }

    @Override
    public List<Focus> queryFocusObject(Integer id,Integer page,Integer count,boolean total) {
        Pagination p = pagination(id,page,count,total,null);
        List<Focus> list = focusMapper.queryFocusObject(p);
        return list;
    }
    public List<Comment> queryMyComment(Integer id,Integer page,Integer count,boolean total){
        Pagination p = pagination(id,page,count,total,1);
        List<Comment> list = commentMapper.queryMyComment(p);
        return list;
    }
    public List<Comment> queryMyCommentReply(Integer id, Integer page, Integer count, boolean total){
        Pagination p = pagination(id,page,count,total,3);
        List<Comment> list = commentMapper.queryMyCommentReply(p);
        return list;
    }
    public List<Like> queryMyLike(Integer id, Integer page, Integer count, boolean total){
        Pagination p = pagination(id,page,count,total,null);
        List<Like> list = likeMapper.queryMyLike(p);
        return list;
    }

    Pagination pagination(Integer id,Integer page,Integer count,boolean total,Integer type){
        Pagination pagination = new Pagination();
        pagination.setUserId(id);
        pagination.setPage(page);
        pagination.setCount(count);
        pagination.setTotal(total);
        pagination.setType(type);
        return pagination;
    }
    public int deleteLike(Integer id, Integer userId){
        return likeMapper.deleteLike(id, userId);
    }

}
